package com.hydong.novatest.entity;

import android.text.TextUtils;

import com.hydong.novatest.base.Constants;
import com.hydong.novatest.Utils.StartTools;

import net.sourceforge.novaforjava.api.LnEquPosn;
import net.sourceforge.novaforjava.api.LnHrzPosn;
import net.sourceforge.novaforjava.api.LnhEquPosn;

import java.util.ArrayList;
import java.util.List;

/**
 * 星体实体，用于Adapter等
 */
public class StarObject {
    private final static String FORMAT_RA = "%dh %dm %ds";
    private final static String FORMAT_DEC = "%s%d° %d′ %d″";

    private StarRecordBean starRecordBean;              // 天体详情，来自数据库等
    private LnEquPosn jNow = new LnEquPosn();           // Double型
    private LnhEquPosn jNowFormat = new LnhEquPosn();   // HMS/DMS型
    private List<LnHrzPosn> altitudeList;               // 方位和高度角

    public StarObject(StarRecordBean bean) {
        this.starRecordBean = bean;
        jNow = StartTools.getInstance().transJ200ToJNow(bean.getRa(), bean.getDec());
        jNowFormat = StartTools.getInstance().formatJNow(jNow);
    }

    public StarRecordBean getStarRecordBean() {
        return starRecordBean;
    }

    /**
     * 获取天体名称
     *
     * @return
     */
    public String getName() {
        if (starRecordBean == null) {
            return "";
        }
        String name = "";
        String category = starRecordBean.getCategory();
        if (category.equals(Constants.STAR_M) || category.equals(Constants.STAR_NGC) || category.equals(Constants.STAR_IC) || category.equals(Constants.STAR_SH2) || category.equals(Constants.STAR_C)) {
            name = category + starRecordBean.getNo();
            if (!TextUtils.isEmpty(starRecordBean.getNoExt())) {
                name += starRecordBean.getNoExt();
            }

            String remark = starRecordBean.getNameEN();
            if (Constants.isChineseEnv) {
                remark = starRecordBean.getNameCN();
            }

            if (!TextUtils.isEmpty(remark)) {
                name += "(" + remark + ")";
            }
        } else {
            if (Constants.isChineseEnv) {
                name = starRecordBean.getNameCN();
            } else {
                name = starRecordBean.getNameEN();
            }
        }
        return name;
    }

    /**
     * 获取 RA Jnow 数值
     *
     * @return
     */
    public double getRaJnow() {
        return jNow.ra;
    }

    /**
     * 获取 DEC Jnow 数值
     *
     * @return
     */
    public double getDecJnow() {
        return jNow.dec;
    }

    /**
     * 获取 RA Jnow字符串格式
     *
     * @return
     */
    public String getRaJnowStr() {
        return String.format(FORMAT_RA, jNowFormat.ra.hours, jNowFormat.ra.minutes, (int) jNowFormat.ra.seconds);
    }

    /**
     * 获取 DEC Jnow字符串格式
     *
     * @return
     */
    public String getDecJnowStr() {
        String sign = "-";
        if (jNowFormat.dec.neg == 0) {
            sign = "+";
        }
        return String.format(FORMAT_DEC, sign, jNowFormat.dec.degrees, jNowFormat.dec.minutes, (int) jNowFormat.dec.seconds);
    }

    /**
     * 获取天体的 方位 和 高度角
     *
     * @return
     */
    public List<LnHrzPosn> getAltitudeList() {
        if (altitudeList == null || altitudeList.size() == 0) {
            altitudeList = new ArrayList<>();
            altitudeList.addAll(StartTools.getInstance().calAltitudeList(jNow));
        }
        return altitudeList;
    }
}
